package com.yicong.taxi.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.StringRes;
import android.view.inputmethod.InputMethod;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;
import com.hjq.toast.ToastUtils;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;
import com.yicong.taxi.R;
import com.yicong.taxi.utils.AppManagerUtil;
import com.yicong.taxi.utils.Density;

import butterknife.ButterKnife;

public abstract class BaseActivity<T extends BaseActivityPresenter> extends RxAppCompatActivity {

  protected String TAG = this.getClass().getSimpleName();
  protected T presenter;

  protected Context mContext;

  protected Activity mActivity;
  protected ImmersionBar immersionBar;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Density.setCustomDensity(this, getApplication());
    doBeforeSetcontentView();
    setContentView(getLayoutResId());
    ButterKnife.bind(this);
    mContext = this.getApplicationContext();
    mActivity = this;
    initPresenter();

    //沉浸式状态栏
//    immersionBar = ImmersionBar.with(this);
//    setImmersionBar();

//    TextView tvBack = findViewById(R.id.tv_back);
//    if(tvBack!=null){
//      tvBack.setOnClickListener(v -> finish());
//    }
//    TextView tvTitle = findViewById(R.id.tv_title);
//    if(tvTitle!=null){
//      tvTitle.setText(setTitle());
//    }
  }

  protected void setImmersionBar(){
//    immersionBar.statusBarColor(R.color.blue_4f).init();
  }

  /**
   * 设置layout前配置
   */
  protected void doBeforeSetcontentView() {
    // 把actvity放到application栈中管理
    AppManagerUtil.getAppManager().addActivity(this);
  }

  protected abstract int getLayoutResId();

  protected abstract void initPresenter();

  public String setTitle(){
   return "" ;
  }

  /**
   * 显示键盘
   */
  protected void showKeyBoard() {
    InputMethodManager imm = (InputMethodManager) this.getApplicationContext()
        .getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(0, InputMethod.SHOW_FORCED);
  }

  /**
   * 隐藏键盘
   */
  protected boolean hideKeyBoard() {
    //		return mBaseActivityImpl.hideKeyBoard();
    final InputMethodManager imm = (InputMethodManager) this.getApplicationContext()
        .getSystemService(Context.INPUT_METHOD_SERVICE);
    return imm.hideSoftInputFromWindow(this.findViewById(android.R.id.content)
        .getWindowToken(), 0);
  }

  public void showError() {
    ToastUtils.show(R.string.error_message);
  }

  public void showToast(String msg) {
    ToastUtils.show(msg);
  }

  public void showToast(@StringRes int res) {
    ToastUtils.show(mActivity.getResources().getString(res));
  }

  public void showLoadingDialog() {
//    LoadingDialogUtil.showProgressDialog(this);
  }

  public void cancelLoadingDialog() {
//    LoadingDialogUtil.cancelProgressDialog();
  }

  @Override
  protected void onRestart() {
    super.onRestart();
  }

  @Override
  protected void onStart() {
    super.onStart();
  }

  @Override
  protected void onResume() {
    super.onResume();
  }

  @Override
  protected void onPause() {
    super.onPause();
  }

  @Override
  protected void onStop() {
    super.onStop();
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    hideKeyBoard();
    ButterKnife.bind(this).unbind();
    AppManagerUtil.getAppManager().finishActivity(this);
    if(presenter!=null){
      presenter.onDestory();
      presenter=null;
      System.gc();
    }
    if(immersionBar!=null){
      immersionBar.destroy();
    }
  }
}
